package com.ruoyi.web.controller.fantasy;

import java.util.Date;
import java.util.List;

import com.huanzhidadi.fantasy.bean.domain.FantasyCity;
import com.huanzhidadi.fantasy.bean.domain.FantasyCityMap;
import com.huanzhidadi.fantasy.bean.domain.FantasyMapShop;
import com.huanzhidadi.fantasy.bean.domain.FantasyShop;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.fantasy.domain.FantasyNpc;
import com.ruoyi.fantasy.domain.FantasyNpcPosition;
import com.ruoyi.fantasy.domain.FantasyShopBO;
import com.ruoyi.fantasy.service.*;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import org.springframework.web.servlet.ModelAndView;

/**
 * 城市地点 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-05
 */
@Controller
@RequestMapping("/fantasy/fantasyCitymap")
public class FantasyCitymapController extends BaseController
{
    private String prefix = "fantasy/fantasyCitymap";

	@Autowired
	private IFantasyCitiesService fantasyCitiesService;

	@Autowired
	private IFantasyCitymapService fantasyCitymapService;

	@Autowired
	private IFantasyNpcPositionService fantasyNpcPositionService;

	@Autowired
	private IFantasyNpcService fantasyNpcService;

	@Autowired
	private IFantasyShopService fantasyShopService;

	@Autowired
	private IFantasyMapShopService fantasyMapShopService;

	@RequiresPermissions("fantasy:fantasyCitymap:view")
	@GetMapping()
	public String fantasyCitymap(ModelMap mmap)
	{
		ModelAndView mv = new ModelAndView();
		List<FantasyCity> cities = fantasyCitiesService.selectFantasyCitiesList(null);
		mmap.addAttribute("cities", cities);
	    return prefix + "/fantasyCitymap";
	}
	
	/**
	 * 查询城市地点列表
	 */
	@RequiresPermissions("fantasy:fantasyCitymap:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(FantasyCityMap fantasyCitymap)
	{
		startPage();
        List<FantasyCityMap> list = fantasyCitymapService.selectFantasyCitymapList(fantasyCitymap);
		fantasyCitymapService.transforData(list);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出城市地点列表
	 */
	@RequiresPermissions("fantasy:fantasyCitymap:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FantasyCityMap fantasyCitymap)
    {
    	List<FantasyCityMap> list = fantasyCitymapService.selectFantasyCitymapList(fantasyCitymap);
        ExcelUtil<FantasyCityMap> util = new ExcelUtil<FantasyCityMap>(FantasyCityMap.class);
        return util.exportExcel(list, "fantasyCitymap");
    }
	
	/**
	 * 新增城市地点
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		List<FantasyCity> cities = fantasyCitiesService.selectFantasyCitiesList(null);
		mmap.addAttribute("cities", cities);

		List<FantasyCityMap> maps = fantasyCitymapService.selectFantasyCitymapList(null);
		fantasyCitymapService.transforData(maps);
		mmap.addAttribute("maps", maps);

	    return prefix + "/add";
	}
	
	/**
	 * 新增保存城市地点
	 */
	@RequiresPermissions("fantasy:fantasyCitymap:add")
	@Log(title = "城市地点", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(FantasyCityMap fantasyCitymap)
	{
		// 取身份信息
		SysUser user = ShiroUtils.getSysUser();
		fantasyCitymap.setCreateBy(user.getUserId().toString());
		fantasyCitymap.setCreateTime(new Date());
		fantasyCitymap.setUpdateBy(user.getUserId().toString());
		fantasyCitymap.setUpdateTime(new Date());
		return toAjax(fantasyCitymapService.insertFantasyCitymap(fantasyCitymap));
	}

	/**
	 * 修改城市地点
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		List<FantasyCity> cities = fantasyCitiesService.selectFantasyCitiesList(null);
		mmap.addAttribute("cities", cities);

		List<FantasyCityMap> maps = fantasyCitymapService.selectFantasyCitymapList(null);
		fantasyCitymapService.transforData(maps);
		mmap.addAttribute("maps", maps);

		FantasyCityMap fantasyCitymap = fantasyCitymapService.selectFantasyCitymapById(id);
		mmap.put("fantasyCitymap", fantasyCitymap);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存城市地点
	 */
	@RequiresPermissions("fantasy:fantasyCitymap:edit")
	@Log(title = "城市地点", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(FantasyCityMap fantasyCitymap)
	{
		SysUser user = ShiroUtils.getSysUser();
		fantasyCitymap.setUpdateBy(user.getUserId().toString());
		fantasyCitymap.setUpdateTime(new Date());
		return toAjax(fantasyCitymapService.updateFantasyCitymap(fantasyCitymap));
	}
	
	/**
	 * 删除城市地点
	 */
	@RequiresPermissions("fantasy:fantasyCitymap:remove")
	@Log(title = "城市地点", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(fantasyCitymapService.deleteFantasyCitymapByIds(ids));
	}

	/**
	 * 禁用城市地点
	 */
	@RequiresPermissions("fantasy:fantasyCitymap:disable")
	@Log(title = "城市地点", businessType = BusinessType.DISABLE)
	@PostMapping( "/disable")
	@ResponseBody
	public AjaxResult disable(String ids)
	{
		return toAjax(fantasyCitymapService.disableFantasyCitymapByIds(ids));
	}

	/**
	 * 还原城市地点
	 */
	@RequiresPermissions("fantasy:fantasyCitymap:disable")
	@Log(title = "城市地点", businessType = BusinessType.RESTORE)
	@PostMapping( "/restore")
	@ResponseBody
	public AjaxResult restore(String ids)
	{
		return toAjax(fantasyCitymapService.restoreFantasyCitymapByIds(ids));
	}


	/**
	 * NPC@城市地点
	 */
	@GetMapping("/npc/{id}")
	public String npc(@PathVariable("id") String id, ModelMap mmap)
	{
		FantasyCityMap fantasyCitymap = fantasyCitymapService.selectFantasyCitymapById(id);
		mmap.put("fantasyCitymap", fantasyCitymap);
		List<FantasyNpc> fantasyNpcList = fantasyNpcService.selectFantasyNpcList(null);
		//获取该地点的npc列表
		FantasyNpcPosition fantasyNpcPosition = new FantasyNpcPosition();
		fantasyNpcPosition.setMapId(id);
		List<FantasyNpcPosition> fantasyNpcPositionList = fantasyNpcPositionService.selectFantasyNpcPositionList(fantasyNpcPosition);
		for (FantasyNpcPosition fantasyNpcPosition1 : fantasyNpcPositionList) {
			for (FantasyNpc fantasyNpc : fantasyNpcList) {
				if (fantasyNpc.getNpcId().equals(fantasyNpcPosition1.getNpcId())) {
					fantasyNpcPosition1.setNpcName(fantasyNpc.getNpcName());
				}
			}
		}
		mmap.put("fantasyNpcPositionList", fantasyNpcPositionList);

		return prefix + "/npc";
	}

	/**
	 * 新增Npc
	 */
	@GetMapping("/addNpc/{id}")
	public String addNpc(@PathVariable("id") String id, ModelMap mmap)
	{
		List<FantasyNpc> fantasyNpcList = fantasyNpcService.selectFantasyNpcList(null);
		mmap.addAttribute("fantasyNpcList", fantasyNpcList);
		mmap.addAttribute("mapId", id);

		return prefix + "/addNpc";
	}

	/**
	 * 新增保存当前地点的Npc
	 */
	@RequiresPermissions("fantasy:fantasyCitymap:add")
	@Log(title = "Npc分配管理", businessType = BusinessType.INSERT)
	@PostMapping("/addNpc")
	@ResponseBody
	public AjaxResult addNpc(FantasyNpcPosition fantasyNpcPosition)
	{
		// 取身份信息
		SysUser user = ShiroUtils.getSysUser();
		fantasyNpcPosition.setPositionId(String.valueOf(IdWorker.nextId()));
		fantasyNpcPosition.setCreateBy(user.getUserId().toString());
		fantasyNpcPosition.setCreateTime(new Date());
		return toAjax(fantasyNpcPositionService.insertFantasyNpcPosition(fantasyNpcPosition));
	}

	/**
	 * 地图节点管理-分配Npc
	 */
	@GetMapping("/allocNpc/{id}")
	public String allocNpc(@PathVariable("id") String id, ModelMap mmap)
	{
		FantasyCityMap fantasyCitymap = fantasyCitymapService.selectFantasyCitymapById(id);
		mmap.put("fantasyCitymap", fantasyCitymap);
		return prefix + "/allocNpc";
	}

	/**
	 * 查询已分配的Npc列表
	 */
	@RequiresPermissions("fantasy:fantasyNpcPosition:list")
	@PostMapping("/allocNpc/allocatedList")
	@ResponseBody
	public TableDataInfo allocatedList(FantasyNpc fantasyNpc)
	{
		startPage();
		List<FantasyNpc> fantasyNpcList = fantasyNpcService.selectAllocatedList(fantasyNpc);
		fantasyNpcService.transforData(fantasyNpcList);
		return getDataTable(fantasyNpcList);
	}

	/**
	 * 取消分配
	 */
	@Log(title = "Npc分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocNpc/cancel")
	@ResponseBody
	public AjaxResult cancelAllocNpc(FantasyNpcPosition fantasyNpcPosition)
	{
		return toAjax(fantasyNpcPositionService.deleteFantasyNpcPosition(fantasyNpcPosition));
	}

	/**
	 * 批量取消分配
	 */
	@Log(title = "Npc分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocNpc/cancelAll")
	@ResponseBody
	public AjaxResult cancelAllocNpcAll(String mapId, String npcIds)
	{
		return toAjax(fantasyNpcPositionService.deleteAllocNpcs(mapId, npcIds));
	}

	/**
	 * 选择Npc
	 */
	@GetMapping("/allocNpc/selectNpc/{mapId}")
	public String selectNpc(@PathVariable("mapId") String mapId, ModelMap mmap)
	{
		FantasyCityMap fantasyCitymap = fantasyCitymapService.selectFantasyCitymapById(mapId);
		mmap.put("fantasyCitymap", fantasyCitymap);
		return prefix + "/selectNpc";
	}

	/**
	 * 查询未分配Npc列表
	 */
	@RequiresPermissions("fantasy:fantasyNpcPosition:list")
	@PostMapping("/allocNpc/unallocatedList")
	@ResponseBody
	public TableDataInfo unallocatedList(FantasyNpc fantasyNpc)
	{
		startPage();
		List<FantasyNpc> list = fantasyNpcService.selectUnallocatedList(fantasyNpc);
		fantasyNpcService.transforData(list);
		return getDataTable(list);
	}

	/**
	 * 批量选择用户授权
	 */
	@RequiresPermissions("fantasy:fantasyNpcPosition:add")
	@Log(title = "Npc分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocNpc/selectAll")
	@ResponseBody
	public AjaxResult selectAllocNpcAll(String mapId, String npcIds)
	{
		SysUser user = ShiroUtils.getSysUser();
		return toAjax(fantasyNpcPositionService.insertAllocNpcs(mapId, npcIds, user));
	}













/*

	*/
/**
	 * 新增Npc
	 *//*

	@GetMapping("/addNpc/{id}")
	public String addNpc(@PathVariable("id") String id, ModelMap mmap)
	{
		List<FantasyNpc> fantasyNpcList = fantasyNpcService.selectFantasyNpcList(null);
		mmap.addAttribute("fantasyNpcList", fantasyNpcList);
		mmap.addAttribute("mapId", id);

		return prefix + "/addNpc";
	}

	*/
/**
	 * 新增保存当前地点的Npc
	 *//*

	@RequiresPermissions("fantasy:fantasyCitymap:add")
	@Log(title = "Npc分配管理", businessType = BusinessType.INSERT)
	@PostMapping("/addNpc")
	@ResponseBody
	public AjaxResult addNpc(FantasyNpcPosition fantasyNpcPosition)
	{
		// 取身份信息
		SysUser user = ShiroUtils.getSysUser();
		fantasyNpcPosition.setPositionId(String.valueOf(IdWorker.nextId()));
		fantasyNpcPosition.setCreateBy(user.getUserId().toString());
		fantasyNpcPosition.setCreateTime(new Date());
		return toAjax(fantasyNpcPositionService.insertFantasyNpcPosition(fantasyNpcPosition));
	}
*/

	/**
	 * 地图节点管理-分配商店
	 */
	@GetMapping("/allocShop/{id}")
	public String allocShop(@PathVariable("id") String id, ModelMap mmap)
	{
		FantasyCityMap fantasyCitymap = fantasyCitymapService.selectFantasyCitymapById(id);
		mmap.put("fantasyCitymap", fantasyCitymap);
		return prefix + "/allocShop";
	}

	/**
	 * 查询已分配的商店列表
	 */
	@RequiresPermissions("fantasy:fantasyShop:list")
	@PostMapping("/allocShop/allocatedShopList")
	@ResponseBody
	public TableDataInfo allocatedShopList(FantasyShopBO fantasyShopBO)
	{
		startPage();
		List<FantasyShop> fantasyShopList = fantasyShopService.selectAllocatedList(fantasyShopBO);
		fantasyShopService.transforData(fantasyShopList);
		return getDataTable(fantasyShopList);
	}

	/**
	 * 取消分配
	 */
	@Log(title = "商店分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocShop/cancel")
	@ResponseBody
	public AjaxResult cancelAllocShop(FantasyMapShop fantasyMapShop)
	{
		return toAjax(fantasyMapShopService.deleteFantasyMapShop(fantasyMapShop));
	}

	/**
	 * 批量取消分配
	 */
	@Log(title = "商店分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocShop/cancelAll")
	@ResponseBody
	public AjaxResult cancelAllocShopAll(String mapId, String shopIds)
	{
		return toAjax(fantasyMapShopService.deleteAllocShops(mapId, shopIds));
	}

	/**
	 * 选择商店
	 */
	@GetMapping("/allocShop/selectShop/{mapId}")
	public String selectShop(@PathVariable("mapId") String mapId, ModelMap mmap)
	{
		FantasyCityMap fantasyCitymap = fantasyCitymapService.selectFantasyCitymapById(mapId);
		mmap.put("fantasyCitymap", fantasyCitymap);
		return prefix + "/selectShop";
	}

	/**
	 * 查询未分配商店列表
	 */
	@RequiresPermissions("fantasy:fantasyShop:list")
	@PostMapping("/allocShop/unallocatedList")
	@ResponseBody
	public TableDataInfo unallocatedList(FantasyShopBO fantasyShopBO)
	{
		startPage();
		List<FantasyShop> list = fantasyShopService.selectUnallocatedList(fantasyShopBO);
		fantasyShopService.transforData(list);
		return getDataTable(list);
	}

	/**
	 * 批量选择分配商店
	 */
	@RequiresPermissions("fantasy:fantasyShop:add")
	@Log(title = "商店分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocShop/selectAll")
	@ResponseBody
	public AjaxResult selectAllocShopAll(String mapId, String shopIds)
	{
		return toAjax(fantasyMapShopService.insertAllocShops(mapId, shopIds));
	}
}
